#include "Fitxers.h"
/*
 * Tret de http://stackoverflow.com/questions/190229/where-is-the-itoa-function-in-linux
 */ 
char * my_itoa(int nVal, char* cpBuf)
{
    const unsigned int radix = 10;

    char* cpP;
    unsigned int a;        //every digit
    int nLen;
    char* cpB;            //start of the digit char
    char cTemp;
    unsigned int u;

    cpP = cpBuf;

    if (nVal < 0) {
        *cpP++ = '-';
        nVal = 0 - nVal;
    }
    u = (unsigned int)nVal;

    cpB = cpP;

    do {
        a = u % radix;
        u /= radix;

        *cpP++ = a + '0';

    } while (u > 0);

    nLen = (int)(cpP - cpBuf);

    *cpP-- = 0;

    //swap
    do {
        cTemp = *cpP;
        *cpP = *cpB;
        *cpB = cTemp;
        --cpP;
        ++cpB;

    } while (cpB < cpP);

    //return len;
    return cpBuf;
}

void copiaJugador (Jugador *stDst, Jugador *stOrg) {
	
	strcpy(stDst->nom, stOrg->nom);
	stDst->total_tirades = stOrg->total_tirades;
	stDst->casella_acrual = stOrg->casella_acrual;
	stDst->tirada_acrual = stOrg->tirada_acrual;
	stDst->posicio = stOrg->posicio;
	stDst->espera = stOrg->espera;
	stDst->color = stOrg->color;
	
}               
     
void bombolla (Jugador stData[], int nNumEl){
    int i;
    int nModif = 1;
    Jugador stAux;
       while (nModif) {
               nModif = 0;

               for (i = 0; i < nNumEl-1; i++) {
                    if (stData[i].total_tirades > stData[i+1].total_tirades){
						
						//intercanvia posicio i i i+1
						copiaJugador(&stAux, &stData[i]);
                        copiaJugador(&stData[i], &stData[i+1]);
                        copiaJugador(&stData[i+1], &stAux);
                        
                        nModif = 1;
                       }
               }
       }

}
void bombolla_jugadors (Jugador stData[], int nNumEl){
    int i;
    int nModif = 1;
    Jugador stAux;
       while (nModif) {
               nModif = 0;

               for (i = 0; i < nNumEl-1; i++) {
                    if (stData[i].posicio < stData[i+1].posicio){

						//intercanvia posicio i i i+1
						copiaJugador(&stAux, &stData[i]);
                        copiaJugador(&stData[i], &stData[i+1]);
                        copiaJugador(&stData[i+1], &stAux);


                        nModif = 1;
                       }
               }
       }

}
